package com.huawei.chengdu.meetingdemo.view.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.device.idea.meeting.ConfManage.model.BookConfInfo;
import com.huawei.chengdu.meetingdemo.R;
import com.huawei.chengdu.meetingdemo.presenter.ConfSdkPresenter;
import com.huawei.chengdu.meetingdemo.util.CallTimeUtils;

import java.util.ArrayList;
import java.util.List;

public class BookConfAdapter extends RecyclerView.Adapter<BookConfAdapter.ConfListViewHolder> {

    private final List<BookConfInfo> bookConfInfoList;

    public BookConfAdapter(List<BookConfInfo> confList) {
        bookConfInfoList = new ArrayList<>();
        updateConfInfoList(confList);
    }

    public void updateConfInfoList(List<BookConfInfo> newConfList) {
        bookConfInfoList.clear();
        bookConfInfoList.addAll(newConfList);
        notifyDataSetChanged();
    }
    @NonNull
    @Override
    public ConfListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_view_book_conf, parent, false);
        return new ConfListViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ConfListViewHolder holder, int position) {
        BookConfInfo bookConfInfo = bookConfInfoList.get(position);
        holder.confName.setText(bookConfInfo.getConferenceName());
        holder.confId.setText(bookConfInfo.getConferenceID());
        holder.beginTime.setText(CallTimeUtils.callTimeToString(bookConfInfo.getBeginTime()));
        holder.endTime.setText(CallTimeUtils.callTimeToString(bookConfInfo.getEndTime()));
        holder.joinConf.setOnClickListener(v -> ConfSdkPresenter.startCall(bookConfInfo.getConferenceID()));
    }

    @Override
    public int getItemCount() {
        return bookConfInfoList.size();
    }

    public class ConfListViewHolder extends RecyclerView.ViewHolder {
        private TextView confName;
        private TextView confId;
        private TextView beginTime;
        private TextView endTime;
        private TextView joinConf;

        public ConfListViewHolder(@NonNull View itemView) {
            super(itemView);
            confName = itemView.findViewById(R.id.conf_name);
            confId = itemView.findViewById(R.id.conf_id);
            beginTime = itemView.findViewById(R.id.conf_begin_time);
            endTime = itemView.findViewById(R.id.conf_end_time);
            joinConf = itemView.findViewById(R.id.join_conf);
        }
    }
}
